/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.frame.pm.utils;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.frame.pm.entity.PmDataDictItem;
import com.thinkgem.jeesite.frame.pm.service.PmDataDictItemService;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 字典工具类
 *
 * @author kongxiangsheng
 * @version 2013-5-29
 */
public class PmDataDictUtils {
    private static PmDataDictItemService pmDataDictItemService = SpringContextHolder.getBean(PmDataDictItemService.class);

    /**
     * 获取字典条目信息
     *
     * @param itemCode
     * @param dictCode
     * @return
     */
    public static PmDataDictItem getDataDictItem(String dictCode, String itemCode) {
        PmDataDictItem item = pmDataDictItemService.getByItemCode(dictCode, itemCode);
        if (null == item) {
            return new PmDataDictItem();
        }
        return item;
    }

    /**
     * 获取单个字典值-查不到时返回字典条目编码
     *
     * @param itemCode
     * @param dictCode
     * @return
     */
    public static String getDataDictValue(String dictCode, String itemCode) {
        PmDataDictItem item = pmDataDictItemService.getByItemCode(dictCode, itemCode);
        if (null == item) {
            return itemCode;
        }
        return item.getItemName();
    }

    /**
     * 获取单个字典值-查不到时返回默认值
     *
     * @param itemCode
     * @param dictCode
     * @param defaultItemName
     * @return
     */
    public static String getDataDictValue(String dictCode, String itemCode, String defaultItemName) {
        PmDataDictItem item = pmDataDictItemService.getByItemCode(dictCode, itemCode);
        if (null == item) {
            return defaultItemName;
        }
        return item.getItemName();
    }

    /**
     * 获取多个字典值-查不到时返回字典条目编码
     *
     * @param itemCodes
     * @param dictCode
     * @return
     */
    public static String getDataDictValues(String dictCode, String itemCodes) {
        List<String> valueList = Lists.newArrayList();
        for (String itemCode : StringUtils.split(itemCodes, ",")) {
            valueList.add(getDataDictValue(dictCode, itemCode));
        }
        return StringUtils.join(valueList, ",");
    }

    /**
     * 根据字典条目的值获取字典条目的Code
     *
     * @param itemName
     * @param dictCode
     * @param defaultItemCode
     * @return
     */
    public static String getDataDictCode(String dictCode, String itemName, String defaultItemCode) {
        List<PmDataDictItem> list = pmDataDictItemService.getByDictCode(dictCode);
        for (PmDataDictItem item : list) {
            if (itemName.equals(item.getItemName())) {
                return item.getItemCode();
            }
        }
        return defaultItemCode;
    }

    /**
     * 根据字典编码获取所有的字典条目
     *
     * @param dictCode
     * @return
     */
    public static List<PmDataDictItem> getDataDictItemList(String dictCode) {
        return pmDataDictItemService.getByDictCode(dictCode);
    }

    /**
     * 返回字典列表（JSON）
     *
     * @param dictCode
     * @return
     */
    public static String getDataDictItemListJson(String dictCode) {
        return JsonMapper.toJsonString(getDataDictItemList(dictCode));
    }

}
